﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmInsTypeInfo : Form
    {
        int _InsTypeID;
        InsTypeInfo _InsInfo;
        DialogResult dlgResult;
        InsTypeClient _wcfInsType;
        RoleRightInfo listRole;
        bool isChange = false;
        public frmInsTypeInfo(int InsTypeID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            //  LoadLanguage();
            _InsTypeID = InsTypeID;
            _wcfInsType = Service.InsType();
            dlgResult = DialogResult.Cancel;
            _InsInfo = new InsTypeInfo();

            if (_InsTypeID > 0)
            {
                _InsInfo = _wcfInsType.InsType_GetInfo(_InsTypeID);
                Info2Form();
            }
            listRole = _listRole;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole.Update)
                btnSave.Enabled = false;
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            label1.Text = UpdateUI.GetString(label1.Tag.ToString());
        }
        private void Info2Form()
        {
            txtCode.Text = _InsInfo.Code;
            txtName.Text = _InsInfo.Name;
            isChange = false;

        }
        void Form2Info()
        {
            _InsInfo.ID = _InsTypeID;
            _InsInfo.Code = txtCode.Text;
            _InsInfo.Name = txtName.Text;
        }
        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            if (txtCode.Text.Trim().Length == 0)
            {
                txtCode.Focus();
                MessageBox.Show(UpdateUI.GetString("chuanhaptongiao"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_InsInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _InsInfo = new InsTypeInfo();
            _InsTypeID = 0;
            txtCode.Text = "";
            txtName.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            _wcfInsType = Service.InsType();
            Form2Info();
            if (_InsTypeID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (_wcfInsType.InsType_Update(_InsInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _InsTypeID = _InsInfo.ID = _wcfInsType.InsType_Insert(_InsInfo);
                if (_InsTypeID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void frmInsTypeInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show("Dữ liệu đã thay đổi.\n Bạn có muốn lưu trước khi thoát không?", UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            _wcfInsType.Close();
            this.DialogResult = dlgResult;
        }

        private void frmInsTypeInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

    }
}
